package com.mengchengnet.chaperonage.cacheservice;


import com.mengchengnet.chaperonage.BaseActivity;
import com.mengchengnet.chaperonage.domain.base.City;
import com.mengchengnet.chaperonage.util.PreferenceUtil;
import com.mengchengnet.chaperonage.util.ValueUtil;

import java.util.ArrayList;

/**
 * 城市选择的数据服务类
 */

public class CitySvc {

    private final static ArrayList<City> CITY_LIST = new ArrayList<>();

    private static City selectedCity;

    public static ArrayList<City> getCityList() {
        return CITY_LIST;
    }

    public static void setCityList(ArrayList<City> cityList) {
        CITY_LIST.clear();
        CITY_LIST.addAll(cityList);
    }

    public static void setSelectedCity(String cityId) {

        for (City city : CITY_LIST) {
            if (city.getCityId().equals(cityId)) {
                selectedCity = city;
                PreferenceUtil.putString(BaseActivity.currentActivity, "selectedCityId", selectedCity.getCityId());
                break;
            }
        }
    }

    public static City getSelectedCity() {

        if (selectedCity == null) {

            String selectedCityId = PreferenceUtil.getString(BaseActivity.currentActivity, "selectedCityId");

            if (ValueUtil.isStrEmpty(selectedCityId)) {
                setSelectedCity("00");
            } else {
                setSelectedCity(selectedCityId);
            }

        }

        return selectedCity;
    }
}
